import luckysheetFreezen from '../controllers/freezen';
import { luckysheet_searcharray } from '../controllers/sheetSearch';
import { luckysheetrefreshgrid, jfrefreshgrid_rhcw } from '../global/refresh';
import Store from '../store';
import method from '../global/method';
import { luckysheetextendData, handleFormulaAndFilterExtend } from '../global/extend';
import { getcellFormula } from './getdata';
import formula from './formula';
import editor from './editor';
import { getSheetIndex } from '../methods/get';
import { getMaxRowLen } from './getRowlen';
import sheetmanage from '../controllers/sheetmanage';
import { selectHightlightShow } from '../controllers/select';
import { exitEditMode, getCellPostion, getCurrentSheetIndex, isCellVisible } from './api';

let scrollRequestAnimationFrameIni = true, scrollRequestAnimationFrame = false, scrollTimeOutCancel = null;

function execScroll () {
    let scrollLeft = $("#luckysheet-scrollbar-x").scrollLeft(),
        scrollTop = $("#luckysheet-scrollbar-y").scrollTop();
    luckysheetrefreshgrid(scrollLeft, scrollTop);
    scrollRequestAnimationFrame = window.requestAnimationFrame(execScroll);
}

// 全局滚动事件
export default function luckysheetscrollevent (isadjust) {
    let $t = $("#luckysheet-cell-main");
    let scrollLeft = $("#luckysheet-scrollbar-x").scrollLeft(),
        scrollTop = $("#luckysheet-scrollbar-y").scrollTop(),
        canvasHeight = $("#luckysheetTableContent").height(); // canvas高度
    // clearTimeout(scrollTimeOutCancel);

    // scrollTimeOutCancel = setTimeout(() => {
    //     scrollRequestAnimationFrameIni  = true;
    //     window.cancelAnimationFrame(scrollRequestAnimationFrame);
    // }, 500);

    // if (!!isadjust) {
    //     let scrollHeight = $t.get(0).scrollHeight;
    //     let windowHeight = $t.height();
    //     let scrollWidth = $t.get(0).scrollWidth;
    //     let windowWidth = $t.width();

    //     let maxScrollLeft = scrollWidth - windowWidth;
    //     let maxScrollTop = scrollHeight - windowHeight;

    //     let visibledatacolumn_c = Store.visibledatacolumn, visibledatarow_c = Store.visibledatarow;

    //     if (luckysheetFreezen.freezenhorizontaldata != null) {
    //         visibledatarow_c = luckysheetFreezen.freezenhorizontaldata[3];
    //     }

    //     if (luckysheetFreezen.freezenverticaldata != null) {
    //         visibledatacolumn_c = luckysheetFreezen.freezenverticaldata[3];
    //     }

    //     let col_ed = luckysheet_searcharray(visibledatacolumn_c, scrollLeft);
    //     let row_ed = luckysheet_searcharray(visibledatarow_c, scrollTop);

    //     let refreshLeft = scrollLeft , refreshTop = scrollTop;

    //     if (col_ed <= 0) {
    //         scrollLeft = 0;
    //     }
    //     else {
    //         scrollLeft = visibledatacolumn_c[col_ed - 1];
    //     }

    //     if (row_ed <= 0) {
    //         scrollTop = 0;
    //     }
    //     else {
    //         scrollTop = visibledatarow_c[row_ed - 1];
    //     }
    // }
    if (Store.luckysheet_select_save.length) {
        let row = Store.luckysheet_select_save[0].row[0];
        let column = Store.luckysheet_select_save[0].column[0];
        let file = Store.luckysheetfile[getCurrentSheetIndex()];
        if (file.dataVerification && file.dataVerification[`${row}_${column}`] && file.dataVerification[`${row}_${column}`].type === "dropdown") {
            exitEditMode();
            $("#luckysheet-dataVerification-dropdown-List").hide();
        }
    }
    if (luckysheetFreezen.freezenhorizontaldata != null) {
        if (scrollTop < luckysheetFreezen.freezenhorizontaldata[2]) {
            scrollTop = luckysheetFreezen.freezenhorizontaldata[2];
            $("#luckysheet-scrollbar-y").scrollTop(scrollTop);
            return;
        }
    }

    if (luckysheetFreezen.freezenverticaldata != null) {
        if (scrollLeft < luckysheetFreezen.freezenverticaldata[2]) {
            scrollLeft = luckysheetFreezen.freezenverticaldata[2];
            $("#luckysheet-scrollbar-x").scrollLeft(scrollLeft);
            return;
        }
    }

    $("#luckysheet-cols-h-c").scrollLeft(scrollLeft);// 列标题
    $("#luckysheet-rows-h").scrollTop(scrollTop);// 行标题
    $t.scrollLeft(scrollLeft).scrollTop(scrollTop);
    editor.changeCellEditInput();
    // if(scrollRequestAnimationFrameIni && Store.scrollRefreshSwitch){
    //     execScroll();
    //     scrollRequestAnimationFrameIni = false;
    // }

    luckysheetrefreshgrid(scrollLeft, scrollTop);

    $("#luckysheet-bottom-controll-row").css("left", scrollLeft);
    if (Store.filterMenuShow) {
        Store.filterMenuShow = false;
        $("#luckysheet-filter-menu, #luckysheet-filter-submenu").hide();
    }

    // 有选区且有冻结时，滚动适应
    if (luckysheetFreezen.freezenhorizontaldata != null || luckysheetFreezen.freezenverticaldata != null) {
        luckysheetFreezen.scrollAdapt();
    }
    if (!method.createHookFunction("scroll", { scrollLeft, scrollTop, canvasHeight })) {  }
    // 滚动的距离加上初始的canvas高度减去header的高度等于实际滚动的位置
    // console.log(scrollTop + canvasHeight - Store.columnHeaderHeight, Store.rh_height - Store.cellMainSrollBarSize)
    // if (Math.abs(scrollTop + canvasHeight - Store.columnHeaderHeight - Store.rh_height + Store.cellMainSrollBarSize) < 1) {
    //     lazyLoadSheetData({ accumulate: 0 }).then(() => {
    //         selectHightlightShow();
    //     });
    // }
}
